package cn.jbolt.wechat.admin.autoreply;

import cn.jbolt.core.api.JBoltApiBaseController;
import cn.jbolt.core.base.JBoltMsg;
import cn.jbolt.core.enumutil.JBoltEnum;
import cn.jbolt.core.permission.CheckPermission;
import cn.jbolt.core.permission.UnCheckIfSystemAdmin;
import cn.jbolt.wechat.common.WechatKeywordsType;
import cn.jbolt.wechat.model.WechatKeywords;
import cn.jbolt.wechat.permission.WechatPermissionKey;
import cn.jbolt.wechat.service.JBoltWechatKeywordsService;
import cn.jbolt.wechat.service.JBoltWechatMpinfoService;
import cn.jbolt.wechat.service.JBoltWechatAutoReplyService;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.core.paragetter.Para;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;

/**
 * 微信公众平台自动回复规则中的触发关键词管理
 *
 * @ClassName: WechatKeywordsAdminController
 * @author: JFinal学院-小木 QQ：909854136
 * @date: 2019年7月1日
 */
@UnCheckIfSystemAdmin
@CheckPermission(WechatPermissionKey.WECHAT_AUTOREPLY_KEYWORDS)
@Path("/api/admin/wechatAutoReply/keywords")
public class WechatKeywordsAdminController extends JBoltApiBaseController {
    @Inject
    private JBoltWechatAutoReplyService wechatAutoReplyService;
    @Inject
    private JBoltWechatKeywordsService service;
    @Inject
    private JBoltWechatMpinfoService wechatMpinfoService;

    @Before(WechatKeywordsMgrValidator.class)
    public void datas() {
        Long autoReplyId = getLong(0);
        renderJBoltApiSuccessWithData(service.getListByAutoReplyId(autoReplyId));
    }




    @Before(WechatKeywordsMgrValidator.class)
    public void index() {
        Long autoReplyId = getLong(0);
        Long id = getLong(1);
        Ret checkRet = service.checkCanOpt(autoReplyId);
        if (checkRet.isFail()) {
            renderJBoltApiRet(checkRet);
            return;
        }
        WechatKeywords wechatKeywords = service.findById(id);
        if (wechatKeywords == null) {
            renderJBoltApiFail(JBoltMsg.DATA_NOT_EXIST);
            return;
        }
        Kv checkResultKv = checkRet.getAs("data");
        ;
        Long mpId = checkResultKv.getLong("mpId");
        if (wechatKeywords.getMpId().longValue() != mpId.longValue()) {
            renderJBoltApiFail("参数异常:公众平台mpId");
            return;
        }
        if (wechatKeywords.getAutoReplyId().longValue() != autoReplyId.longValue()) {
            renderJBoltApiFail("参数异常:所属规则 autoReplyId");
            return;
        }
		renderJBoltApiSuccessWithData(wechatKeywords);
    }

    public void types() {
        renderJBoltApiSuccessWithData(JBoltEnum.getEnumOptionList(WechatKeywordsType.class));
    }

    public void save(@Para("autoReplyId") Long autoReplyId, @Para("wechatKeywords") WechatKeywords keywords) {
        renderJBoltApiRet(service.save(autoReplyId, keywords));
    }

    public void update(@Para("autoReplyId") Long autoReplyId, @Para("wechatKeywords") WechatKeywords keywords) {
        renderJBoltApiRet(service.update(autoReplyId, keywords));
    }

    @Before(WechatKeywordsMgrValidator.class)
    public void delete() {
        renderJBoltApiRet(service.delete(getLong(0), getLong(1)));
    }
}
